﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Omnia.DataModels;
using System.Windows.Input;
using Omnia.Library;
using System.Collections.ObjectModel;
using System.Windows;

namespace Omnia.ViewModels
{
    public class CategoryViewModel : MainViewModel
    {
        #region Properties
        private CategoryInfo itemToAdd = new CategoryInfo();
        public CategoryInfo ItemToAdd
        {
            get { return itemToAdd; }
            set
            {
                itemToAdd = value;
                RaisePropertyChanged(() => ItemToAdd);
            }
        }

        public ObservableCollection<CategoryInfo> IncomingCategories
        {
            get
            {
                IEnumerable<CategoryInfo> items = Database.CollectionOfCategories.Where(c => c.Kind == Enums.TransactionKindValue.Income);
                foreach (CategoryInfo item in items)
                {
                    if (Database.CollectionOfTransactions.Count(t => t.CategoryGuid == item.Guid) > 0)
                    {
                        var trans = Database.CollectionOfTransactions.Where(t => t.CategoryGuid == item.Guid);//.Sum(t => t.Amount);
                        double sum = trans.Sum(t => t.Amount);
                        item.Sum = sum;
                        item.Percent = (sum * 100) / TotalIncome;
                        item.TransactionCount = trans.Count();
                    }
                    else
                    {
                        item.Sum = 0;
                        item.Percent = 0;
                        item.TransactionCount = 0;
                    }
                }
                return new ObservableCollection<CategoryInfo>(items);
            }
        }

        public ObservableCollection<CategoryInfo> ExpenseCategories
        {
            get
            {
                IEnumerable<CategoryInfo> items = Database.CollectionOfCategories.Where(c => c.Kind == Enums.TransactionKindValue.Expense);
                foreach (CategoryInfo item in items)
                {
                    if (Database.CollectionOfTransactions.Count(t => t.CategoryGuid == item.Guid) > 0)
                    {
                        var trans = Database.CollectionOfTransactions.Where(t => t.CategoryGuid == item.Guid);//.Sum(t => t.Amount);
                        double sum = trans.Sum(t => t.Amount);
                        item.Sum = sum;
                        item.Percent = (sum * 100) / TotalExpense;
                        item.TransactionCount = trans.Count();
                    }
                    else
                    {
                        item.Sum = 0;
                        item.Percent = 0;
                        item.TransactionCount = 0;
                    }
                }
                return new ObservableCollection<CategoryInfo>(items);
            }
        }

        private Visibility addpanelVisibility = Visibility.Collapsed;
        public Visibility AddpanelVisibility
        {
            get { return addpanelVisibility; }
            set
            {
                addpanelVisibility = value;
                RaisePropertyChanged(() => AddpanelVisibility);
                RaisePropertyChanged(() => OpenPanelCommand);
                RaisePropertyChanged(() => ClosePanelCommand);
            }
        }
        #endregion

        #region Method
        private void Insert(CategoryInfo entity)
        {
            if (entity.Default)
            {
                var item = Database.CollectionOfCategories.FirstOrDefault(c => c.Kind == entity.Kind && c.Default);
                if (item != null)
                {
                    item.Default = false;
                    Save();
                }
            }

            Database.CollectionOfCategories.InsertOnSubmit(entity);
            Save();

            RaisePropertyChanged(() => CategoriesCollection);
            RaisePropertyChanged(() => IncomingCategories);
            RaisePropertyChanged(() => ExpenseCategories);
            ItemToAdd = new CategoryInfo();
        }
        #endregion

        #region Commands
        public ICommand AddCommand
        {
            get
            {
                return new GenericCommand()
                {
                    //CanExecuteCallback = delegate { return string.IsNullOrWhiteSpace(ItemToAdd.Name); },
                    ExecuteCallback = delegate
                    {
                        Insert(ItemToAdd);
                        ClosePanelCommand.Execute(null);
                    }
                };
            }
        }

        public ICommand OpenPanelCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return AddpanelVisibility == Visibility.Collapsed; },
                    ExecuteCallback = delegate
                    {
                        AddpanelVisibility = Visibility.Visible;
                        RaisePropertyChanged(() => AddpanelVisibility);
                    }
                };
            }
        }

        public ICommand ClosePanelCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return AddpanelVisibility == Visibility.Visible; },
                    ExecuteCallback = delegate
                    {
                        AddpanelVisibility = Visibility.Collapsed;
                        RaisePropertyChanged(() => AddpanelVisibility);
                    }
                };
            }
        }
        #endregion
    }
}
